import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 登录数字化平台-创新活动-动态评审
 *
 * @author admin
 */
public class Brush_Szh_review {

    public static void main(String[] args) {
        Brush_Szh_review brushSzhreview = new Brush_Szh_review();
//        testZm.test("fengbinpeng", "student_feng123");
//        testZm.test("fushihao", "16484532522fsh@");
        brushSzhreview.test("dongyuning", "dyn02890@");
    }

    public void test(String username, String password) {
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");


        WebDriver driver = new ChromeDriver();
        Map<String, Object> vars = new HashMap<String, Object>();
        JavascriptExecutor js = (JavascriptExecutor) driver;

        //1.登录
        driver.get("http://iscsso.sgcc.com.cn/isc_sso/login?service=http://open.sgcc.com.cn/pi6000-ua-web/api/daptValidate?target=http://open.sgcc.com.cn");
        driver.manage().window().setSize(new Dimension(1936, 1056));
        driver.findElement(By.id("department")).click();
        driver.findElement(By.className("tab_item2")).click();
        driver.findElement(By.linkText("河南电力")).click();


        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("submit_login")).click();

        //这里需要停顿一下，才能跳转菜单
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//        driver.getPageSource();

        //2.创新活动
        driver.findElement(By.linkText("创新活动")).click();

        //获取一下资源，不然跳转不了活动
        driver.getPageSource();

        vars.put("window_handles", driver.getWindowHandles());

        driver.findElement(By.cssSelector(".is-active img")).click();

        vars.put("root", driver.getWindowHandle());


        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Set<String> whNow = driver.getWindowHandles();
        Set<String> whThen = (Set<String>) vars.get("window_handles");
        if (whNow.size() > whThen.size()) {
            whNow.removeAll(whThen);
        }
        vars.put("win7640", whNow.iterator().next());

        driver.switchTo().window(vars.get("win7640").toString());

        //3.大众评审
        driver.findElement(By.id("tab-0-大众评审")).click();

        //4.搜索名称
        //4.1 数智国网
        driver.findElement(By.id("keywords")).click();
        driver.findElement(By.id("keywords")).sendKeys("数智国网");
        driver.findElement(By.cssSelector(".search-btn")).click();

        driver.findElement(By.cssSelector(".public-judge:nth-child(1) > .judge-text .button span")).click();
        driver.findElement(By.cssSelector(".vote-list-btn > span")).click();
        driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".go-back-btn > span")).click();

        //4.2 数达
        driver.findElement(By.id("keywords")).click();
        driver.findElement(By.id("keywords")).sendKeys("数达");

        driver.findElement(By.cssSelector(".search-btn > span")).click();
        driver.findElement(By.cssSelector(".public-judge:nth-child(1) > .judge-text .button span")).click();
        driver.findElement(By.cssSelector(".vote-list-btn > span")).click();
        driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span")).click();

        {
            WebElement element = driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".go-back-btn > span")).click();


        //4.3 晓数电
        driver.findElement(By.id("keywords")).click();
        driver.findElement(By.id("keywords")).sendKeys("晓数电");

        driver.findElement(By.cssSelector(".search-btn > span")).click();
        driver.findElement(By.cssSelector(".public-judge:nth-child(1) > .judge-text .button span")).click();
        driver.findElement(By.cssSelector(".vote-list-btn > span")).click();
        driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span")).click();
//        driver.findElement(By.cssSelector(".el-button--small > span")).click();

        {
            WebElement element = driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".go-back-btn > span")).click();

        //4.4 数创国网
        driver.findElement(By.id("keywords")).click();
        driver.findElement(By.id("keywords")).sendKeys("数创国网");

        driver.findElement(By.cssSelector(".search-btn > span")).click();
//        driver.findElement(By.cssSelector(".el-button--small > span")).click();
        driver.findElement(By.cssSelector(".public-judge:nth-child(1) > .judge-text .button span")).click();
        driver.findElement(By.cssSelector(".vote-list-btn > span")).click();
        driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span")).click();

        {
            WebElement element = driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".go-back-btn > span")).click();


        //4.5 沃土
        driver.findElement(By.id("keywords")).click();
        driver.findElement(By.id("keywords")).sendKeys("沃土");

        driver.findElement(By.cssSelector(".search-btn > span")).click();
        driver.findElement(By.cssSelector(".public-judge:nth-child(1) > .judge-text .button span")).click();
        driver.findElement(By.cssSelector(".vote-list-btn > span")).click();
        driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".go-back-btn > span")).click();

        //4.6 数智共享平台
        driver.findElement(By.id("keywords")).click();
        driver.findElement(By.id("keywords")).sendKeys("数智共享平台");

        driver.findElement(By.cssSelector(".search-btn > span")).click();
        driver.findElement(By.cssSelector(".public-judge:nth-child(1) > .judge-text .button span")).click();
        driver.findElement(By.cssSelector(".vote-list-btn > span")).click();
        driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".vote-btn-box:nth-child(1) span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".go-back-btn > span")).click();

        //
        driver.findElement(By.cssSelector(".public-judge:nth-child(2) .button span")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".public-judge:nth-child(2) .button span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".vote-list-btn > span")).click();
        js.executeScript("window.scrollTo(0,400)");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //确认投票
        driver.findElement(By.cssSelector(".vote-btn-box:nth-child(3) span")).click();
        //确认
        driver.findElement(By.cssSelector(".el-button:nth-child(2) > span")).click();

//        driver.close();

        //关闭浏览器
//        driver.switchTo().window(vars.get("root").toString());
//        driver.close();

//        driver.switchTo().window(vars.get("undefined").toString());

    }

}
